package com.zjx.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class LogAspect {

	public LogAspect(){
		System.out.println("LogAspect...");
	}
    @Pointcut("execution(* com.zjx.aop.HelloService.sayHello(..))")
	private void pt1(){}

	@Around("pt1()")
	public void logAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		String name = proceedingJoinPoint.getSignature().getName();


		System.out.println("环绕通知之前==>"+name+"....【args: "+ Arrays.asList(proceedingJoinPoint.getArgs()) +"】");
		proceedingJoinPoint.proceed();
		System.out.println("环绕通知之后==>"+name+"....【args: "+ Arrays.asList(proceedingJoinPoint.getArgs()) +"】");
	}
	@Before("pt1()")
	public void logStart(JoinPoint joinPoint){
		String name = joinPoint.getSignature().getName();
		System.out.println("前置通知==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】");
	}

	@AfterReturning(value = "execution(* com.zjx.aop.HelloService.sayHello(..))",returning = "result")
	public void logReturn(JoinPoint joinPoint,Object result){
		String name = joinPoint.getSignature().getName();
		System.out.println("正常返回==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】【result: "+result+"】");
	}

	@After("pt1()")
	public void logEnd(JoinPoint joinPoint){
		String name = joinPoint.getSignature().getName();
		System.out.println("后置通知==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】");
	}

	@AfterThrowing(value = "execution(* com.zjx.aop.HelloService.sayHello(..))",throwing = "e")
	public void logError(JoinPoint joinPoint,Exception e){
		String name = joinPoint.getSignature().getName();
		System.out.println("异常通知==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】【exception: "+e+"】");
	}
}
